/**
 * 作者：陈文斌 on 2018/12/29.
 * 邮箱：chen_wennbin@163.com
 * 版本：v1.0
 */

import 'package:flutter/material.dart';
import 'package:crm/constant/constant.dart';
import 'package:crm/view/GesturePassword/gesture_password.dart';
import 'package:crm/viewModel/Login/login_view_model.dart';
import 'package:crm/view/AppBar/custom_app_bar.dart';

const String Title = '设置手势密码';

enum PromptStyle {
  firstPrompt,
  secondPrompt,
  errorPrompt,
  failPrompt,
}

const Text firstPrompt = Text(
  '请绘制手势密码',
  style: TextStyle(
    color: AppColors.TextColor,
    fontSize: AppFontSizes.Font16,
    fontFamily: AppFontFamilies.PingFangSC,
    fontWeight: FontWeight.w500,
  ),
  textAlign: TextAlign.center,
);

const Text secondPrompt = Text(
  '请再一次绘制手势密码',
  style: TextStyle(
    color: AppColors.TextColor,
    fontSize: AppFontSizes.Font16,
    fontFamily: AppFontFamilies.PingFangSC,
    fontWeight: FontWeight.w500,
  ),
  textAlign: TextAlign.center,
);

const Text errorPrompt = Text(
  '两次绘制手势密码不一致请重新绘制',
  style: TextStyle(
    color: AppColors.PromptColor,
    fontSize: AppFontSizes.Font16,
    fontFamily: AppFontFamilies.PingFangSC,
    fontWeight: FontWeight.w500,
  ),
  textAlign: TextAlign.center,
);

const Text failPrompt = Text(
  '至少连接4个点',
  style: TextStyle(
    color: AppColors.PromptColor,
    fontSize: AppFontSizes.Font16,
    fontFamily: AppFontFamilies.PingFangSC,
    fontWeight: FontWeight.w500,
  ),
  textAlign: TextAlign.center,
);

class SettingGesturePasswordPage extends StatefulWidget {
  final String appBarTitle;

  @override
  SettingGesturePasswordPage({
    Key key,
    this.appBarTitle: Title,
  }) : super(key: key);

  State<StatefulWidget> createState() => _SettingGesturePasswordPageState();
}

class _SettingGesturePasswordPageState
    extends State<SettingGesturePasswordPage> {
  PromptStyle _promptStyle = PromptStyle.firstPrompt;

  // 保存手势密码
  String _pass = '';

  @override
  Widget build(BuildContext context) {
    Widget _promptSection() {
      Text _promptText;
      switch (_promptStyle) {
        case PromptStyle.firstPrompt:
          _promptText = firstPrompt;
          break;
        case PromptStyle.secondPrompt:
          _promptText = secondPrompt;
          break;
        case PromptStyle.errorPrompt:
          _promptText = errorPrompt;
          break;
        case PromptStyle.failPrompt:
          _promptText = failPrompt;
          break;
        default:
          _promptText = firstPrompt;
      }

      return Container(
          width: AppScreen.setWidth(175),
          height: 44,
          margin: EdgeInsets.fromLTRB(
              AppScreen.setWidth(100), 51, AppScreen.setWidth(100), 0),
          child: _promptText);
    }

    Widget _gesturePasswordSection() {
      return Container(
        margin: const EdgeInsets.only(top: 55.0),
        child: GesturePassword(
          successCallback: (s) {
            switch (_promptStyle) {
              case PromptStyle.firstPrompt:
                {
                  _pass = s;
                  _promptStyle = PromptStyle.secondPrompt;
                  setState(() {});
                }
                return;
              case PromptStyle.secondPrompt:
                {
                  if (s == _pass) {
                    // 与第一次密码设置一致，发送给服务端
                    if (widget.appBarTitle == Title) {
                      LoginVM().goSettingPasswordGesture(
                          context: context, gesturePass: _pass);
//                      Navigator.push(
//                        context,
//                        MaterialPageRoute(
//                          builder: (context) => LoginPage(),
//                        ),
//                      );
                    } else {}
                  } else {
                    // 与第一次密码设置不一致，更改提示信息，清空第一次设置的密码重新设置
                    _pass = '';
                    _promptStyle = PromptStyle.errorPrompt;
                    setState(() {});
                  }
                }
                return;
              case PromptStyle.errorPrompt:
                {
                  _pass = s;
                  _promptStyle = PromptStyle.secondPrompt;
                  setState(() {});
                }
                return;
              case PromptStyle.failPrompt:
                {
                  _pass = s;
                  _promptStyle = PromptStyle.secondPrompt;
                  setState(() {});
                }
                return;
              default:
                {
                  _promptStyle = PromptStyle.firstPrompt;
                  setState(() {});
                }
            }
          },
          failCallback: () {
            _promptStyle = PromptStyle.failPrompt;
            setState(() {});
          },
        ),
      );
    }

    return Scaffold(
      backgroundColor: Colors.white,
      appBar: CustomAppBar(
        title: widget.appBarTitle,
      ),
      body: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          _promptSection(),
          _gesturePasswordSection(),
        ],
      ),
    );
  }
}
